package com.eco.fanliapp.controller.app;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
//import com.eco.fanliapp.annotation.SysLogAnnotation;
import com.eco.fanliapp.common.base.BaseController;
import com.eco.fanliapp.common.response.ServerResponse;
import com.eco.fanliapp.entity.AppUser;
import com.eco.fanliapp.entity.SyTimePeriodAppointment;
import com.eco.fanliapp.service.ISyTimePeriodAppointmentService;
import com.eco.fanliapp.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * <p>
 * 预约抢购表 前端控制器
 * </p>
 *
 * @author jobob
 * @since 2019-09-23
 */
@RestController
@RequestMapping("/app/syTimePeriodAppointment")
public class SyTimePeriodAppointmentController extends BaseController {

    @Autowired
    private ISyTimePeriodAppointmentService iSyTimePeriodAppointmentService;


    /**
     * 预约/取消预约 秒杀商品
     * @param syTimePeriodAppointment
     * @return
     */
//    @SysLogAnnotation(value = "预约秒杀商品",userType = 1)
    @PostMapping(value = "save")
    public ServerResponse save(@RequestBody SyTimePeriodAppointment syTimePeriodAppointment) {
        if(syTimePeriodAppointment.getTableId() == null && (syTimePeriodAppointment.getTimePeriodId() == null || syTimePeriodAppointment.getItemId() == null)){
            return ServerResponse.createByErrorMessage("参数有误");
        }
        AppUser appUser = getAppUser();
        syTimePeriodAppointment.setUserId(appUser.getUserId());
        if(syTimePeriodAppointment.getTableId() == null){
            String strDate = DateUtils.date2Str(new Date(), DateUtils.DATE_FORMAT_YYYYMMDD);
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("item_id",syTimePeriodAppointment.getItemId());
            queryWrapper.eq("user_id",syTimePeriodAppointment.getUserId());
            queryWrapper.between("item_price_date",strDate+" 00:00:00",strDate+" 23:59:59");
            int count = iSyTimePeriodAppointmentService.count(queryWrapper);
            if(count>0){
                return  ServerResponse.createByErrorMessage("亲 您已经预约过该商品了");
            }
        }
        syTimePeriodAppointment.setItemPriceDate(new Date());
        syTimePeriodAppointment.setUpdateTime(new Date());
        boolean save = iSyTimePeriodAppointmentService.saveOrUpdate(syTimePeriodAppointment);
        if (save) return ServerResponse.createBySuccessMessage("操作成功");
        return ServerResponse.createByErrorMessage("操作失败");

    }

}
